/*
The MIT License
Copyright (c) 2019 Lehrstuhl Informatik 11 - RWTH Aachen University
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE

This file is part of embeddedRTPS.

Author: i11 - Embedded Software, RWTH Aachen University
*/

#ifndef RTPS_READER_H
#define RTPS_READER_H

#include "rtps/common/types.h"
#include "rtps/config.h"
#include "rtps/discovery/TopicData.h"
#include "rtps/entities/WriterProxy.h"
#include "rtps/storages/MemoryPool.h"
#include "rtps/storages/PBufWrapper.h"

struct rtps_ReaderCacheChange {
	uint8_t *data;
	ChangeKind_t kind;
	DataSize_t size;
	Guid_t writerGuid;
	SequenceNumber_t sn;
};

struct rtps_Reader {
	TopicData m_attributes;
	bool m_is_initialized;
	bool m_bstateful;  /*default is false, stateless*/
	struct rtps_MemoryPool *m_proxies;
};

typedef struct rtps_ReaderCacheChange ReaderCacheChange;
typedef struct rtps_Reader Reader;

typedef void (*ddsReaderCallback_fp)(void *callee,
				     const struct rtps_ReaderCacheChange *cacheChange);

void rtps_ReaderCacheChange_Construct(struct rtps_ReaderCacheChange *readerCC,
				      ChangeKind_t kind, Guid_t *writerGuid, SequenceNumber_t sn,
				      uint8_t *data, DataSize_t size);
bool rtps_Reader_knowWriterId(struct rtps_Reader *reader, const Guid_t *guid);

inline bool rtps_ReaderCacheChange_copyInto(const struct rtps_ReaderCacheChange *readerCC,
				     uint8_t *buffer, DataSize_t destSize)
{
	if (destSize < readerCC->size)
		return false;

	else {
		memcpy(buffer, readerCC->data, readerCC->size);
		return true;
	}
}
inline const uint8_t *rtps_ReaderCacheChange_getData(struct rtps_ReaderCacheChange *readerCC)
{
	return readerCC->data;
}

inline DataSize_t rtps_ReaderCacheChange_getDataSize(
			struct rtps_ReaderCacheChange *readerCC)
{
	return readerCC->size;
}

void rtps_Reader_Construct(struct rtps_Reader *reader);
void rtps_Reader_Destruct(struct rtps_Reader *reader);

inline bool rtps_Reader_isInitialized(struct rtps_Reader *reader)
{
	return reader->m_is_initialized;
}

#endif // RTPS_READER_H
